package seismosurfer.gui.menu;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.help.CSH;
import javax.swing.JMenu;

import com.bbn.openmap.gui.GoToMenu;
import com.bbn.openmap.gui.NavigateMenu;
import com.bbn.openmap.gui.GoToMenu.AddNewViewButton;
import com.bbn.openmap.gui.GoToMenu.GoToButton;
import com.bbn.openmap.gui.menu.OMBasicMenu;

/**
 * An extension to NavigateMenu to customize it 
 * for the Seismo-Surfer applet.
 * This menu holds menu items for the Coordinates
 * dialog box, zoom in/out operations, a submenu
 * for various views (projections) and a submenu
 * to show all the data of a layer.
 *
 */
public class SeismoNavigateMenu extends NavigateMenu {
    
    private static final long serialVersionUID = -8436644602088809955L;

    protected SeismoGoToMenu goToMenu;

    protected JMenu viewsMenu = new JMenu("Views");

    public SeismoNavigateMenu() {
        super();

        remove(3);
        addSeparator();
        add(viewsMenu);

        CSH.setHelpIDString(this, "navigateMenu");
    }
    
    /**
     * Gets the first result of the {@link #getMenuComponents(JMenu, Class)}. 
     * 
     * @param menu menu the JMenu to be checked
     * @param c the Class which is used in the
     *        condition checking
     * @return a list of components
     */
    public Component getMenuComponent(JMenu menu, Class c) {
        List result = getMenuComponents(menu, c);
        return (Component) result.get(0);
    }
    
    /**
     * Checks the given menu for the existence
     * of instances of a given class and
     * returns a list of those instances.
     * 
     * @param menu the JMenu to be checked
     * @param c the Class which is used in the
     *        condition checking
     * @return a list of components
     */
    public List getMenuComponents(JMenu menu, Class c) {
        List result = new ArrayList();
        Component[] items = menu.getMenuComponents();

        for (int i = 0; i < items.length; i++) {
            if (c.isInstance(items[i])) {
                result.add(items[i]);
            }
        }
        return result;
    }

    public void findAndInit(Object someObj) {
        super.findAndInit(someObj);

        if (someObj instanceof GoToMenu) {
            goToMenu = (SeismoGoToMenu) someObj;
            goToMenu.setViewsMenu(viewsMenu);

            goToMenu.setVisible(false);
            addGoToMenuComponents();
        }
    }

    public void findAndUndo(Object someObj) {
        super.findAndUndo(someObj);
    }
    
    protected void addGoToMenuComponents() {
        viewsMenu.add(getAddNewViewMenuItem());
        viewsMenu.addSeparator();

        List result = getMenuComponents(goToMenu, GoToButton.class);
        for (Iterator iter = result.iterator(); iter.hasNext();) {
            GoToButton item = (GoToButton) iter.next();
            viewsMenu.add(item);
        }

        add(getDataBoundsMenu());

        revalidate();
    }
    
    /**
     * Provides a reference to the AddNewViewButton menu item
     * instance which is contained in the views submenu.
     * 
     * @return a reference to the AddNewViewButton menu item
     */
    public AddNewViewButton getAddNewViewMenuItem() {
        return (AddNewViewButton) getMenuComponent(goToMenu,
                AddNewViewButton.class);
    }
    
    /**
     * Provides a reference to the OMBasicMenu submenu
     * instance which is contained in the views submenu.
     * 
     * @return a reference to the OMBasicMenu submenu
     */
    public OMBasicMenu getDataBoundsMenu() {
        return (OMBasicMenu) getMenuComponent(goToMenu, OMBasicMenu.class);
    }

}
